package org.example.util;


import org.example.model.Student;
import org.example.model.Experiment;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {

    // 导入学生数据
    public static List<Student> importStudents(String filePath) {
        // 实际项目中这里会实现Excel读取逻辑
        // 为演示简单返回空列表
        return new ArrayList<>();
    }

    // 导出实验统计结果到Excel
    public static boolean exportExperimentStats(Experiment experiment, List<Student> missingStudents, String filePath) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("实验统计");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = createHeaderStyle(workbook);

            createCell(headerRow, 0, "实验名称", headerStyle);
            createCell(headerRow, 1, "课程名称", headerStyle);
            createCell(headerRow, 2, "截止日期", headerStyle);
            createCell(headerRow, 3, "已提交学生数", headerStyle);
            createCell(headerRow, 4, "应提交学生数", headerStyle);
            createCell(headerRow, 5, "未提交学生数", headerStyle);

            // 填写实验信息
            Row experimentRow = sheet.createRow(1);
            CellStyle dataStyle = workbook.createCellStyle(); // 创建数据单元格样式
            createCell(experimentRow, 0, experiment.getName(), dataStyle);
            createCell(experimentRow, 1, experiment.getCourseName(), dataStyle);
            createCell(experimentRow, 2, experiment.getDueDate(), dataStyle);
            // 假设 experiment.getSubmittedStudents().size() 是已提交学生数
            createCell(experimentRow, 3, "", dataStyle);
            createCell(experimentRow, 4, "", dataStyle);
            createCell(experimentRow, 5, "", dataStyle);

            // 创建未提交学生列表表头
            Row studentsHeaderRow = sheet.createRow(3);
            createCell(studentsHeaderRow, 0, "未提交学生列表", headerStyle);

            // 创建学生信息表头
            Row studentHeaderRow = sheet.createRow(4);
            createCell(studentHeaderRow, 0, "学生ID", headerStyle);
            createCell(studentHeaderRow, 1, "姓名", headerStyle);
            createCell(studentHeaderRow, 2, "班级", headerStyle);
            createCell(studentHeaderRow, 3, "联系方式", headerStyle);

            // 填写学生信息
            CellStyle studentStyle = workbook.createCellStyle(); // 创建学生信息单元格样式
            for (int i = 0; i < missingStudents.size(); i++) {
                Student student = missingStudents.get(i);
                Row studentRow = sheet.createRow(5 + i);
                createCell(studentRow, 0, String.valueOf(student.getId()), studentStyle);
                createCell(studentRow, 1, student.getName(), studentStyle);
                createCell(studentRow, 2, student.getClassName(), studentStyle);
                createCell(studentRow, 3, "", studentStyle); // 假设这里没有联系方式
            }

            // 自动调整列宽
            for (int i = 0; i < 6; i++) {
                sheet.autoSizeColumn(i);
            }

            // 写入文件
            File file = new File(filePath);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                file.createNewFile();
            }

            try (FileOutputStream fileOut = new FileOutputStream(file)) {
                workbook.write(fileOut);
            }

            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static CellStyle createHeaderStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
        style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }

    private static void createCell(Row row, int column, Object value, CellStyle style) {
        Cell cell = row.createCell(column);
        if (value instanceof Integer || value instanceof Long || value instanceof Double) {
            cell.setCellValue((Double) value);
        } else {
            cell.setCellValue(value.toString());
        }
        cell.setCellStyle(style);
    }
}